package controller

import (
	"api_interface/dao"
	"api_interface/entity"
	"api_interface/service"
	"fmt"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

type data struct {
	Total    int                  `json:"total"`
	Userdata []entity.AccountInfo `json:"userdata"`
}

// 登录

type LoginRequest struct {
	Id       string `json:"id" `
	Password string `json:"pwd" `
}

func Login() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		var loginRequest LoginRequest
		c.ShouldBindJSON(&loginRequest)
		remember, _ := strconv.ParseBool(c.DefaultQuery("remember", ""))
		// 调用service层的方法
		code, msg, data := service.Login(loginRequest.Id, remember, loginRequest.Password)
		/*
			错误处理：
			1. 用户不存在
			2. 密码错误
		*/
		if code != 0 {
			if code == 1 {
				msg = "用户不存在"
			} else {
				msg = "密码错误"
			}
			c.JSON(http.StatusBadRequest, gin.H{
				"code": code,
				"msg":  msg,
				"data": data,
			})

		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": code,
				"msg":  msg,
				"data": data,
			})
		}
	}
}

/*
注册
邮箱和密码必须，Netid选填
*/
type RegisterRequest struct {
	Email string `json:"email" `
	Nam   string `json:"nam" `
	Pwd   string `json:"pwd" `
}

func Register() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		var registerRequest RegisterRequest
		c.ShouldBindJSON(&registerRequest)
		// 调用service层的方法
		code, msg := service.Register(registerRequest.Email, registerRequest.Pwd, registerRequest.Nam)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": code,
				"msg":  msg,
			})
			return
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": code,
				"msg":  msg,
			})
		}

	}
}

/*
查询个人信息
*/

func GetInfo() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		// 获取URL路径中的参数
		id := c.DefaultQuery("id", "")
		fmt.Println("id:", id)
		authHeader := c.GetHeader("Authorization")
		var data entity.AccountInfo
		// 调用service层的方法
		fmt.Println(authHeader)
		data, code, msg := service.GetInfo(id, authHeader)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"data": data,
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"data": data,
				"code": code,
				"msg":  msg,
			})
		}

	}

}

// 修改个人信息
type ModifyRequest struct {
	Data entity.AccountInfo `json:"data"`
}

func Modify() gin.HandlerFunc {
	return func(c *gin.Context) {
		var user ModifyRequest
		c.ShouldBindJSON(&user)
		fmt.Println(user)
		code, msg := service.Modify(user.Data, user.Data.Email)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": code,
				"msg":  msg,
			})
		}

	}

}

// 查询关注列表

func GetConcernInfo() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		id := c.DefaultQuery("id", "")
		typ := c.DefaultQuery("typ", "")
		pageNum := c.DefaultQuery("pageNum", "")
		pageSize := c.DefaultQuery("pageSize", "")
		intId, _ := strconv.Atoi(id)
		intTyp, _ := strconv.Atoi(typ)
		intPageNum, _ := strconv.Atoi(pageNum)
		intPageSize, _ := strconv.Atoi(pageSize)
		var d data
		// 调用service层的方法
		var code int
		var msg string
		d.Userdata, d.Total, code, msg = service.GetConcernInfo(intId, intTyp, intPageNum, intPageSize)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"data": d,
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"data": d,
				"code": code,
				"msg":  msg,
			})
		}
	}

}

/*
验证邮箱
错误类型：
 1. 邮箱不存在 -> msg = "email not exist"，code = 1
 2. 邮箱已注册 -> msg = "email has been registered"，code = 2
*/
type Email struct {
	Email string `json:"email" `
}

func deleteCode(Email string) {
	// 如果五分钟内没有验证，则删除验证码
	done := time.After(2 * time.Minute)
	// 阻塞等待通道信号
	<-done
	dao.SqlSession.Model(&entity.AccountInfo{}).Where("email = ?", Email).Update("emailcode", "")
	fmt.Println("验证码已删除")
}

func VerifyEmail() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		//var e Email
		email := c.PostForm("email")
		//if err := c.ShouldBindJSON(&e); err!= nil {
		//fmt.Println("66666666666666666666666666666666")
		//}
		fmt.Println("email:", email)
		// 调用service层的方法
		code, msg := service.VerifyEmail(email)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": code,
				"msg":  msg,
			})
		}

		go deleteCode(email)
	}
}

/*
验证邮箱和验证码
*/
type VerifyEmailAndCodeRequest struct {
	Email string `json:"email" `
	Code  string `json:"code0" `
}

func VerifyEmailAndCode() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		var e VerifyEmailAndCodeRequest
		c.ShouldBindJSON(&e)
		// 调用service层的方法
		code, msg := service.VerifyEmailAndCode(e.Email, e.Code)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": code,
				"msg":  msg,
			})
		}
	}
}

/*
重设密码
*/
type ResetPwdRequest struct {
	Email    string `json:"email"`
	Password string `json:"pwd"`
}

func ResetPwd() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		var req ResetPwdRequest
		c.ShouldBindJSON(&req)
		// 调用service层的方法
		code, msg := service.ResetPwd(req.Email, req.Password)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": code,
				"msg":  msg,
			})
		}

	}
}

/*
查找用户
*/

func FindAccount() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		pageNum := c.DefaultQuery("pageNum", "")
		pageSize := c.DefaultQuery("pageSize", "")
		id := c.DefaultQuery("id", "")
		fmt.Println("id:", id)
		var d data
		var code int
		var msg string
		page, _ := strconv.Atoi(pageNum)
		page_size, _ := strconv.Atoi(pageSize)
		d.Userdata, d.Total, code, msg = service.FindAccount(id)
		if (page+1)*page_size >= d.Total {
			d.Userdata = (d.Userdata)[page*page_size : d.Total]
		} else {
			d.Userdata = (d.Userdata)[page*page_size : (page+1)*page_size]
		}
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"data": d,
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"data": d,
				"code": code,
				"msg":  msg,
			})
		}
	}
}

/*
更改邮箱
*/
type UpdateEmailRequest struct {
	Email string `json:"email"`
	Id    int    `json:"id"`
}

func UpdateEmail() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取请求参数
		var req UpdateEmailRequest
		c.ShouldBindJSON(&req)
		authHeader := c.GetHeader("Authorization")
		fmt.Println("id:", req.Id, "email:", req.Email)
		code, msg := service.UpdateEmail(req.Id, req.Email, authHeader)
		if code != 0 {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": code,
				"msg":  msg,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": code,
				"msg":  msg,
			})
		}
	}
}
